package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Unidadrinde;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOUnidadrinde;

public class MySQLDAOUnidadrinde implements DAOUnidadrinde {
	
	private Unidadrinde unidadrinde = null;
	
	
	public MySQLDAOUnidadrinde() {
	}

	public int insert(Unidadrinde unidadrinde) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Unidadrinde ("
					+ "Id_Unidadrinde, Nombre "
					+ ") " + "values (?, ?) ");

			stmt.setLong(1, unidadrinde.getNroUnidadrinde());
			stmt.setString(2, unidadrinde.getNombre());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Unidadrinde unidadrinde) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update Unidadrinde " 
							+ "set Nombre = ?"
							+ " where id_Unidadrinde = ? ");

			stmt.setString(1, unidadrinde.getNombre());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Unidadrinde unidadrinde) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from Unidadrinde where id_Unidadrinde = ? ");
			stmt.setLong(1, unidadrinde.getNroUnidadrinde());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Unidadrinde> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Unidadrinde> unidadrindes = new ArrayList<Unidadrinde>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from Unidadrinde");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Unidadrinde unidadrinde = new Unidadrinde();
				unidadrinde.setNroUnidadrinde(rs.getInt("Id_Unidadrinde"));
				unidadrinde.setNombre(rs.getString("Nombre"));
				unidadrindes.add(unidadrinde);
			}

			return unidadrindes;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Unidadrinde findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		unidadrinde = new Unidadrinde();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Unidadrinde where Id_Unidadrinde = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   unidadrinde.setNroUnidadrinde(rs.getInt("Id_Unidadrinde"));
            unidadrinde.setNombre(rs.getString("Nombre"));
			}

			return unidadrinde;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Unidadrinde unidadrinde) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Unidadrinde where UCASE(nombre)");

			stmt.setString(1, unidadrinde.getNombre().toUpperCase());
//			stmt.setString(2, unidadrinde.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
